﻿using CommandLine;
using PiCompressor;
using PiCompressor.Console;
using System.Numerics;

await Parser.Default.ParseArguments<Options>(args)
    .WithParsedAsync(async (options) => {
        var compressor = new PiCompressor<string>(new RealPi());

        if (options.DecompressMode)
        {
            var input = await File.ReadAllBytesAsync(options.Input);
            var inputValue = new BigInteger(input);
            var result = await compressor.DecompressAsync(inputValue);
            await File.WriteAllTextAsync(options.Output, result);
        }
        else
        {
            var input = await File.ReadAllTextAsync(options.Input);
            var result = await compressor.CompressAsync(input);
            var resultBytes = result?.ToByteArray() ?? Array.Empty<byte>();
            await File.WriteAllBytesAsync(options.Output, resultBytes);
            if (result is not null)
            {
                var before = new FileInfo(options.Input).Length;
                var after = new FileInfo(options.Output).Length;
                Console.WriteLine($"压缩前：{before}");
                Console.WriteLine($"压缩后：{after}");
                Console.WriteLine($"压缩率：{(double)after / before}");
            }
        }
    });
